const express=require('express')
const router=express.Router()
const pool=require('../pool')
const {host} = require('./host')
const {   
    resSuccessData,
    resSuccess,
    resError} = require('../utils/index')
const query = require('../router/utils')
router.get('/',(req,res)=>{
    pool.query('select * from category',(err,result)=>{
        if(err) throw err
        res.send(result)
    })
})
router.get('/list',(req,res)=>{
    let id=req.query.id
    let page=req.query.page
    let count=6
    let start=(parseInt(page)-1)*count
    let sql=`select * from detail  where c_id=? limit ${start},${count} `
    pool.query(sql,[id],(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+el.photo
        });
        res.send(result)
    })
})
router.get('/detail',(req,res)=>{
    let id=req.query.id
    let sql=`select * from detail  where id=? `
    pool.query(sql,[id],(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+"/"+el.photo
        });
        res.send(result)
    })
})
//添加分类

router.post('/add',async(req,res)=>{
    // 分类中文名称 英文名称
    let {
        category,
        english
    } = req.body;

    //空判
    const obj = {
        category,
        english
    };
    //空判
    try{
        Object.keys(obj)
        .forEach(key=>{
            if(!obj[key]){
                resError(res,`${key}不能为空`);
                throw new Error('')
            }
        })
    }catch(e){
        console.log(e,'cate');
        return
    }
    //sql语句
    let sql = `insert into category (category,english) values (?,?)`;
    const ret = await query(sql,Object.values(obj));
    if(ret.affectedRows>0){
        obj.id = ret.insertId;
        resSuccessData(res,obj)
 
    }else{
     resError(res,'插入失败');
    }
})
//删除
router.delete('/',async(req,res)=>{
     // 传入数组
     let dels = req.body;
     // console.log(dels)
     if(dels.length){
         dels = dels.join(',');
         // 删除
         let sql = `delete from category where id in(${dels})`;
         const ret = await query(sql,[]);
         if(ret.affectedRows>0){
             resSuccess(res)
         }else{
             resError(res,'删除失败')
         }
     }else{
         resError(res,'删除参数不能为空')
     }
})
//修改
router.put('/',async(req,res)=>{
     // 分类中文名称 英文名称
     let {
        category,
        english,
        id
    } = req.body;

    //空判
    const obj = {
        category,
        english,
        id
    };
    //空判
    try{
        Object.keys(obj)
        .forEach(key=>{
            if(!obj[key] && key=='id'){
                resError(res,`${key}不能为空`);
                throw new Error('')
            }
        })
    }catch(e){
        console.log(e,'cate');
        return
    }
    //sql语句
    let sql = `update category set category=?,english=? where id=?`;
    const ret = await query(sql,Object.values(obj));
    if(ret.affectedRows>0){
        resSuccessData(res,obj)
 
    }else{
     resError(res,'修改失败');
    }
})
module.exports=router